package cn.edu.zafu.bigdata.x.sxy;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class StudentManagementSystem{
    private List<Student> students;
    private final String FILE_NAME = "studnet.dat";

    public StudentManagementSystem(){
        students = loadstudents();
    }
    // 创建学生表
    private List<Student> loadstudents(){
        List<Student> studentList = new ArrayList<>();
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME))){
            studentList = (List<Student>) ois.readObject();
        } catch (FileNotFoundException e){
            // no deal
        } catch (IOException | ClassNotFoundException e){
            e.printStackTrace();
        }
        return studentList;
    }

    //写入文件
    private void saveStudent(){
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))){
            oos.writeObject(students);
        } catch (IOException e){
            e.printStackTrace();
        }
    }
    // 添加学生
    private void addStudent(){
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入学号：");
        String id = scanner.nextLine();
        for (Student student : students){
            if (student.getId().equals(id)){
                System.out.println("学号重复，请重新输入。");
                return;
            }
        }
        System.out.print("请输入姓名：");
        String name = scanner.nextLine();
        System.out.print("请输入电话：");
        String phone = scanner.nextLine();
        students.add(new Student(id,name,phone));
        saveStudent();
        System.out.println("添加成功");
    }
    // 寻找学生
    private void serarchStudnet(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("已进入查找模式，可输入quit退出。");
        while (true){
            System.out.print("请输入要查找的学号：");
            String id = scanner.nextLine();
            if(id.equalsIgnoreCase("quit")) break;
            boolean found = false;
            for (Student student:students){
                if(student.getId().equals(id)){
                    System.out.println(student);
                    found = true;
                    break;
                }
            }
            if(!found){
                System.out.println("未找到该学生");
            }
        }
    }
    // 更新学生信息
    private void updateStudent(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("已进入更新模式，可输入quit退出。");
        while (true){
            System.out.print("请输入需要更新学生的学号：");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("quit")) break;
            boolean found = false;
            for (Student student:students){
                if(student.getId().equals(id)){
                    found = true;
                    System.out.print("请输入姓名：");
                    student.setName(scanner.nextLine());
                    System.out.print("请输入电话：");
                    student.setPhone(scanner.nextLine());
                    System.out.println("更新完成");
                    break;
                }
            }
            if(!found){
                System.out.println("未找到该学生");
            }
        }
    }
    // 删除学生
    private void deleteStudent(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("注意，已进入删除模式，可输入quit退出。");
        while (true){
            System.out.print("请输入需要被删除的学生学号：");
            String id = scanner.nextLine();
            if (id.equalsIgnoreCase("quit")) break;
            boolean found = false;
            for (Student student:students){
                if(student.getId().equals(id)){
                    found = true;
                    // 二次确认
                    System.out.println("该学生信息如下：");
                    System.out.print(student);
                    System.out.print("是否确认删除？[yes/no]:");
                    String yn = scanner.nextLine();
                    if (yn.equals("no")){
                        break;
                    }
                    students.remove(student);
                    saveStudent();
                    System.out.println("学生信息已删除。");
                    break;
                }
            }
            if(!found){
                System.out.println("未找到该学生");
            }
        }
    }
    //展示学生信息
    public void showStudent(){
        int count = 0;
        for (Student student:students){
            System.out.print(student);
            count++;
        }
        System.out.println("展示完毕，共有学生"+count+"名。");
    }

    public void start(){
        Scanner scanner = new Scanner(System.in);
        while (true){
            System.out.println("=======学生信息管理系统=======");
            System.out.println("1.添加学生信息");
            System.out.println("2.检索学生信息");
            System.out.println("3.更新学生信息");
            System.out.println("4.删除学生信息");
            System.out.println("5.显示当前学生信息");
            System.out.println("6.退出程序");
            int choice = scanner.nextInt();
            scanner.nextLine();
            switch (choice){
                case 1:
                    addStudent();
                    break;
                case 2:
                    serarchStudnet();
                    break;
                case 3:
                    updateStudent();
                    break;
                case 4:
                    deleteStudent();
                    break;
                case 5:
                    showStudent();
                    break;
                case 6:
                    saveStudent();
                    System.out.println("程序结束");
                    return;
                default:
                    System.out.println("无效输入，请重新输入。");
            }
        }
    }

    public static void main(String[] args) {
        StudentManagementSystem system = new StudentManagementSystem();
        system.start();
    }
}
